package com.tju.resource.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tju.resource.common.ServiceException;
import com.tju.resource.mapper.TbConsumablesAmountMapper;
import com.tju.resource.mapper.TbConsumablesMapper;
import com.tju.resource.mapper.TbOutwardConsumablesMapper;
import com.tju.resource.model.TbConsumables;
import com.tju.resource.model.TbConsumablesAmount;
import com.tju.resource.model.TbOutwardConsumables;
import com.tju.resource.server.TbConsumablesService;
import com.tju.resource.tool.result.Result;
import com.tju.resource.tool.result.ResultEnum;
import com.tju.resource.tool.result.ResultUtil;
import com.tju.resource.tool.tool.ClientParameters;
import com.tju.resource.tool.tool.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TbConsumablesServiceImpl implements TbConsumablesService {

    @Autowired
    TbConsumablesMapper tbConsumablesMapper;
    @Autowired
    TbConsumablesAmountMapper tbConsumablesAmountMapper;
    @Autowired
    TbOutwardConsumablesMapper tbOutwardConsumablesMapper;

    /**
     * 查询
     *
     * @param parameters
     * @return
     */
    @Override
    public Result<Object> getList(ClientParameters parameters) {
        try {
            Page<TbConsumables> page = new Page<>(parameters.getPage(), parameters.getLimit());
            IPage<TbConsumables> iPage = tbConsumablesMapper.getList(page, parameters);
            return ResultUtil.success(ResultEnum.SUCCESS, iPage.getRecords(), iPage.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }

    /**
     * 添加
     *
     * @param tbConsumables
     * @return
     */
    @Override
    public Result<Object> add(TbConsumables tbConsumables) {
        QueryWrapper<TbConsumables> wrapper = new QueryWrapper<>();
        wrapper.eq("name", tbConsumables.getName());
        if (EmptyUtils.isEmpty(tbConsumablesMapper.selectOne(wrapper))) {
            if (tbConsumablesMapper.insert(tbConsumables) > 0) {
                if (tbConsumablesAmountMapper.insert(new TbConsumablesAmount(tbConsumables.getId(), tbConsumables.getAmount(), tbConsumables.getCreateBy())) > 0) {
                    return ResultUtil.success(ResultEnum.SUCCESS, "添加成功！");
                }
            }
        } else
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "耗材名称已存在！");
        throw new ServiceException("添加失败！");
    }

    /**
     * 删除
     *
     * @param tbConsumables
     * @return
     */
    @Override
    public Result<Object> del(TbConsumables tbConsumables) {
        QueryWrapper<TbOutwardConsumables> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("consumables_id", tbConsumables.getId());
        if (EmptyUtils.isNotEmpty(tbOutwardConsumablesMapper.selectList(queryWrapper))) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "外发已使用当前耗材，删除失败！");
        }
        if (tbConsumablesMapper.deleteById(tbConsumables.getId()) > 0) {
            QueryWrapper<TbConsumablesAmount> wrapper = new QueryWrapper<>();
            wrapper.eq("consumables_id", tbConsumables.getId());
            tbConsumablesAmountMapper.delete(wrapper);
            return ResultUtil.success(ResultEnum.SUCCESS, "删除成功！");
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, "删除失败！");
    }

    /**
     * 修改
     *
     * @param tbConsumables
     * @return
     */
    @Override
    public Result<Object> upd(TbConsumables tbConsumables) {
        System.err.println(tbConsumables);
        QueryWrapper<TbConsumables> wrapper = new QueryWrapper<>();
        wrapper.eq("name", tbConsumables.getName());
        TbConsumables consumables = tbConsumablesMapper.selectOne(wrapper);
        if (EmptyUtils.isNotEmpty(consumables)) {
            if (!consumables.getId().equals(tbConsumables.getId()))
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "耗材名称已存在！");
        }
        if (tbConsumablesMapper.updateById(tbConsumables) > 0) {
            TbConsumablesAmount tbConsumablesAmount = tbConsumablesAmountMapper.getAmount(tbConsumables.getId());
            if (EmptyUtils.isNotEmpty(tbConsumablesAmount))
                if (tbConsumablesAmount.getAmount() != tbConsumables.getAmount()) {
                    if (tbConsumablesAmountMapper.insert(new TbConsumablesAmount(tbConsumables.getId(), tbConsumables.getAmount(), tbConsumables.getUpdateBy())) > 0) {
                        return ResultUtil.success(ResultEnum.SUCCESS, "修改成功！");
                    }
                }
            return ResultUtil.success(ResultEnum.SUCCESS, "修改成功！");
        }
        throw new ServiceException("修改失败！");
    }
}
